<?php

namespace app\common\service;

use app\common\job\MiniProgramMessage;
use app\common\model\Member;
use think\facade\Queue;

/**
 * 微信小程序订阅消息
 * Class MiniProgramMessageService
 * @package app\common\service
 */
class MiniProgramMessageService implements SubscribeMessageService
{

    /**
     * 律师回复咨询问题
     * @param $memberId
     * @param $content
     * @param $lawyerName
     * @param $time
     * @return void
     */
    public function messageLawyerFirstReplay($memberId, $content, $lawyerName, $time)
    {
        $touser = Member::where('id', $memberId)->value('openidWeiXin');
        if (is_blank($touser)) {
            return;
        }

        $template_id = 'SIMCSIs6qoyQH9EAmzYF7b4h--zm6ZflZFN6SPz_lls';
        $data = [
            'thing5' => ['value' => mb_substr($content, 0, 20)],
            'thing6' => ['value' => $lawyerName],
            'time8' => ['value' => date('Y年m月d日 H:i:s', $time)],
        ];
        $page = 'pages/user/user?idx=3';

        $this->pushMessage($touser, $template_id, $data, $page);
    }

    /**
     * 提醒用户评价
     * @param $memberId
     * @param $content
     * @param $lawyerName
     * @return void
     */
    public function messageRemindComment($memberId, $content, $lawyerName)
    {
        $touser = Member::where('id', $memberId)->value('openidWeiXin');
        if (is_blank($touser)) {
            return;
        }

        $thing3 = '律师解答不易，评价鼓励一下ta吧！';

        $template_id = 'IrDMcK38SowX9sZy-7gLlKn-gjweY9by_RYbVVwLF2c';
        $data = [
            'thing1' => ['value' => mb_substr($content, 0, 20)],
            'thing2' => ['value' => $lawyerName],
            'thing3' => ['value' => $thing3],
        ];
        $page = 'pages/user/user?idx=3';

        $this->pushMessage($touser, $template_id, $data, $page);

    }

    public function getConsultingTemplates(): array
    {
        return [
            'SIMCSIs6qoyQH9EAmzYF7b4h--zm6ZflZFN6SPz_lls',
            'IrDMcK38SowX9sZy-7gLlKn-gjweY9by_RYbVVwLF2c'
        ];
    }

    private function pushMessage($touser, $template_id, $data, $page)
    {
        $job_data = [
            'touser' => $touser,
            'template_id' => $template_id,
            'data' => $data,
            'page' => $page,
        ];

        Queue::push(MiniProgramMessage::class, $job_data);
    }
}